package two;

import java.util.Random;
import java.util.Scanner;



public class MonsterGame {

    static class Player {
        int hp;
        int attack;
        int defense;  // 防御力
        int intelligence;  // 智力
        int stamina;  // 体力
        int dodge;  // 躲避
        int level; // 等级
        int exp; // 经验值
        int skillPoints; // 技能点
        int gold; // 金币

        // ... 其他属性 ...
        int fireMagicLevel; // 火焰魔法等级
        int healMagicLevel; // 治疗魔法等级
        Player(int hp, int attack, int defense, int intelligence, int stamina, int dodge) {
            this.hp = hp;
            this.attack = attack;
            this.defense = defense;
            this.intelligence = intelligence;
            this.stamina = stamina;
            this.dodge = dodge;
            this.level = 1;
            this.exp = 0;
            this.skillPoints = 0;
            this.gold = 0;
            // ... 初始化其他属性 ...
            this.fireMagicLevel = 0;
            this.healMagicLevel = 0;
        }

        // ... 其他方法 ...


        public void gainExp(int exp) {
            this.exp += exp;
            if (this.exp >= this.level * 30) { // 升级条件
                this.level++;
                this.skillPoints += 5; // 升级获得技能点
                this.defense += 5; // 升级提升防御力
                this.intelligence += 5; // 升级提升智力
                this.stamina += 10; // 升级提升体力
                this.dodge += 5; // 升级提升躲避

                System.out.println("你升到了 " + this.level + " 级！");
                // ... 升级提升属性值 ...
            }
        }

        // 学习技能
        public void learnSkill(int skillChoice) {
            if (skillChoice == 1) { // 学习火焰魔法
                if (this.skillPoints >= 5) {
                    this.fireMagicLevel++;
                    this.skillPoints -= 5;
                    System.out.println("你学会了火焰魔法！当前等级：" + this.fireMagicLevel);
                } else {
                    System.out.println("技能点不足，无法学习火焰魔法！");
                }
            } else if (skillChoice == 2) { // 学习治疗魔法
                if (this.skillPoints >= 5) {
                    this.healMagicLevel++;
                    this.skillPoints -= 5;
                    System.out.println("你学会了治疗魔法！当前等级：" + this.healMagicLevel);
                } else {
                    System.out.println("技能点不足，无法学习治疗魔法！");
                }
            } else {
                System.out.println("无效的技能选择！");
            }
        }
        // 使用火焰魔法
        public void useFireMagic(Monster monster) {
            if (this.fireMagicLevel > 0) {
                int damage = this.intelligence * this.fireMagicLevel; // 智力影响魔法伤害
                monster.hp -= damage;
                System.out.println("你使用了火焰魔法，造成了 " + damage + " 点伤害！");
            } else {
                System.out.println("你还没有学习火焰魔法！");
            }
        }
        // 使用治疗魔法
        public void useHealMagic() {
            if (this.healMagicLevel > 0) {
                this.hp += 50 * this.healMagicLevel;
                System.out.println("你使用了治疗魔法，恢复了 " + (50 * this.healMagicLevel) + " 点生命值！");
            } else {
                System.out.println("你还没有学习治疗魔法！");
            }
        }
        // 攻击方法
        public void attack(Monster monster) {

            Random random = new Random();

            if (random.nextInt(100) < monster.dodge) {
                System.out.println(monster.name + " 躲避了你的攻击！");
            } else {
                int damage = this.attack - monster.defense;
                if (damage > 0) {
                    monster.hp -= damage;
                    System.out.println("你攻击了 " + monster.name + "，造成了 " + damage + " 点伤害！");
                } else {
                    System.out.println("你的攻击被 " + monster.name + " 防御住了！");
                }
            }
        }




    }

    static class Monster {
        int level=1;
        int hp;
        int attack;
        int defense;  // 防御力
        int intelligence;  // 智力
        int stamina;  // 体力
        int dodge;  // 躲避
        String name;
        int gold; // 金币

        Monster(int hp, int attack, int defense, int intelligence, int stamina, int dodge, String name, int gold) {
            this.hp = hp;
            this.attack = attack;
            this.defense = defense;
            this.intelligence = intelligence;
            this.stamina = stamina;
            this.dodge = dodge;
            this.name = name;
            this.gold = gold;
        }

        // 攻击方法
        public void attack(Player player) {

            Random random = new Random();

            if (random.nextInt(100) < player.dodge) {
                System.out.println("你躲避了 " + this.name + " 的攻击！");
            } else {
                int damage = this.attack - player.defense;
                if (damage > 0) {
                    player.hp -= damage;
                    System.out.println(this.name + " 攻击了你，造成了 " + damage + " 点伤害！");
                } else {
                    System.out.println(this.name + " 的攻击被你防御住了！");
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        // 创建玩家
        Player player = new Player(100, 35, 5, 30, 60, 30);  // 设置玩家属性

        while (player.hp > 0) { // 当玩家还有生命值时，继续战斗

            Monster monster = createMonster(random); // 创建怪物

            System.out.println("你遇到了一个 " + monster.name + "！");

            while (player.hp > 0 && monster.hp > 0) {
                // 玩家回合
                System.out.println("你的 HP：" + player.hp + "，" + monster.name + " 的 HP：" + monster.hp);
                System.out.println("请选择你的行动：1. 攻击 2. 使用技能 3. 使用道具 4. 查看状态 5. 商店 6. 学习技能");
                int choice = scanner.nextInt();

                if (choice == 1) {
                    // 玩家攻击
                    player.attack(monster);
                } else if (choice == 2) {
                    // 玩家使用技能
                    useSkill(player, monster, scanner);

                } else if (choice == 3) {
                    // 玩家使用道具
                    useItem(player, scanner);

                } else if (choice == 4) {
                    // 查看状态
                    displayPlayerStatus(player);

                } else if (choice == 5) {

                    // 进入商店
                    enterShop(player, scanner);

                } else if (choice == 6) {
                    // 学习技能
                    System.out.println("请选择要学习的技能：1. 火焰魔法 2. 治疗魔法");
                    int skillChoice = scanner.nextInt();
                    player.learnSkill(skillChoice);
                } else {
                    System.out.println("无效的选项！");
                }

                // 怪物回合
                if (monster.hp > 0) {
                    // 躲避判定
                    monster.attack(player);
                }
            }

            handleBattleResult(player, monster); // 处理战斗结果

        }
        System.out.println("游戏结束！");
        scanner.close();
    }

    // 创建怪物的方法
    private static Monster createMonster(Random random) {
        int monsterType = random.nextInt(3);
        switch (monsterType) {
            case 0: return new Monster(50, 10, 3, 5, 15, 5, "史莱姆", 10);
            case 1: return new Monster(70, 15, 7, 8, 20, 10, "哥布林", 20);
            case 2: return new Monster(100, 20, 10, 5, 25, 15, "狼人", 30);
            default: return null; // 应该添加异常处理
        }
    }

    // 处理战斗结果的方法
    private static void handleBattleResult(Player player, Monster monster) {
        if (monster.hp <= 0) {
            System.out.println("你战胜了 " + monster.name + "！ ");
            player.gainExp(monster.level * 10);
            player.gold += monster.gold;
            player.skillPoints += 5;
        } else {
            System.out.println("你被 " + monster.name + " 打败了！");
        }
    }

    // 进入商店的方法
    private static void enterShop(Player player, Scanner scanner) {
        System.out.println("欢迎来到商店！当前金币：" + player.gold);
        System.out.println("1. 治疗药水 (10 金币)");
        System.out.println("2. 攻击药水 (20 金币)");
        System.out.println("请选择你想要购买的物品：");
        int shopChoice = scanner.nextInt();
        if (shopChoice == 1) {
            // 治疗药水
            if (player.gold >= 10) {
                player.gold -= 10;
                System.out.println("你购买了一瓶治疗药水！");
            } else {
                System.out.println("你的金币不足，无法购买治疗药水！");
            }
        } else if (shopChoice == 2) {
            // 攻击药水
            if (player.gold >= 20) {
                player.gold -= 20;
                System.out.println("你购买了一瓶攻击药水！");
            } else {
                System.out.println("你的金币不足，无法购买攻击药水！");
            }
        } else {
            System.out.println("无效的商店选择！");
        }
    }

    // 使用道具的方法
    private static void useItem(Player player, Scanner scanner) {
        System.out.println("请选择你的道具：1. 治疗药水 2. 攻击药水");
        int itemChoice = scanner.nextInt();
        if (itemChoice == 1) {
            // 治疗药水
            if (player.gold >= 10) {
                player.hp += 50;
                player.gold -= 10;
                System.out.println("你使用了治疗药水，恢复了 50 点生命值！");
            } else {
                System.out.println("你的金币不足，无法购买治疗药水！");
            }
        } else if (itemChoice == 2) {
            // 攻击药水
            if (player.gold >= 20) {
                player.attack += 15;
                player.gold -= 20;
                System.out.println("你使用了攻击药水，提升了 15 点攻击力！");
            } else {
                System.out.println("你的金币不足，无法购买攻击药水！");
            }
        } else {
            System.out.println("无效的道具选择！");
        }
    }

    // 查看状态的方法
    private static void displayPlayerStatus(Player player) {
        System.out.println("你的状态：");
        System.out.println("HP：" + player.hp);
        System.out.println("攻击力：" + player.attack);
        System.out.println("防御力：" + player.defense);
        System.out.println("智力：" + player.intelligence);
        System.out.println("体力：" + player.stamina);
        System.out.println("躲避：" + player.dodge);
        System.out.println("等级：" + player.level);
        System.out.println("经验值：" + player.exp);
        System.out.println("技能点：" + player.skillPoints);
        System.out.println("金币：" + player.gold);
    }

    // 使用技能的方法
    private static void useSkill(Player player, Monster monster, Scanner scanner) {
        System.out.println("请选择你的技能：1. 治疗 2. 火焰魔法");
        int skillChoice = scanner.nextInt();
        if (skillChoice == 1) {
            player.useHealMagic();
        } else if (skillChoice == 2) {
            player.useFireMagic(monster);
        } else {
            System.out.println("无效的技能选择！");
        }
    }
}